/*
	Twitch
	Please leave credits to the origional devs where its due if edited.
*/

ENT.Type				= "anim"
ENT.Base				= "base_anim"
ENT.Author				= "Twitch Team"
ENT.PrintName			= "Player ship"
ENT.Category			= "Twich"
ENT.Spawnable			= false
ENT.AdminSpawnable		= false

function ENT:SetupDataTables()
	self:DTVar("Int",0,"Class")
	self:DTVar("Int",1,"Health")
	self:DTVar("Int",2,"MaxHealth")
	self:DTVar("Float",0,"Energy")
	self:DTVar("Float",1,"MaxEnergy")
	self:DTVar("Float",2,"Shield")
	self:DTVar("Float",3,"MaxShield")
	self:DTVar("Bool",0,"ThirdPerson")
	self:DTVar("Entity",0,"Player")
end

function ENT:Think()
	local ply = self:GetPlayer()
	if SERVER then
		local Acc = 3
		if ply:KeyDown(IN_SPEED) then
			self.TargetSpeed = math.Clamp(self.TargetSpeed+Acc,-20,self.MaxSpeed)
		end
		if ply:KeyDown(IN_DUCK) then
			self.TargetSpeed = math.Clamp(self.TargetSpeed-Acc,-20,self.MaxSpeed)
		end
		if self.NextShieldGenerate <= CurTime() then
			self:SetShield(self:Shield()+0.3)
			self.NextShieldGenerate = CurTime()+0.1
		end
		if self.NextEnergyGenerate <= CurTime() then
			self:SetEnergy(self:Energy()+0.3)
			self.NextEnergyGenerate = CurTime()+0.05
		end
	end
	if ply:KeyDown(IN_ATTACK) then
		self:PrimaryAttack()
	end
	if ply:KeyDown(IN_ATTACK2) then
		self:SecondaryAttack()
	end
end

function ENT:PrimaryAttack()
	if self.LastPrimaryFire or 0 <= CurTime() then
		local Func = GAMEMODE:GetWeaponFunction(self:GetPrimaryWeapon())
		if type(Func) == "function" then
			self.LastPrimaryFire = CurTime()+(Func(self) or 1)
		end
	end
end

function ENT:SecondaryAttack()
	if self.LastSecondaryFire or 0 <= CurTime() then
		local Func = GAMEMODE:GetWeaponFunction(self:GetSecondaryWeapon())
		if type(Func) == "function" then
			self.LastSecondaryFire = CurTime()+(Func(self) or 1)
		end
	end
end

function ENT:Health()
	return self.dt.Health
end

function ENT:Shield()
	return self.dt.Shield
end

function ENT:Energy()
	return self.dt.Energy
end

function ENT:GetPlayer()
	return self.dt.Player
end

function ENT:MaxHealth()
	return self.dt.MaxHealth
end

function ENT:MaxShield()
	return self.dt.MaxShield
end

function ENT:MaxEnergy()
	return self.dt.MaxEnergy
end

function ENT:ThirdPerson()
	return self.dt.ThirdPerson == true
end

function ENT:GetPrimaryWeapon()
	return self:GetNWString("TWC_PrimaryWeapon")
end

function ENT:GetSecondaryWeapon()
	return self:GetNWString("TWC_SecondaryWeapon")
end

function ENT:GetSpeed()
	return math.floor(self:GetVelocity():Length())
end
